package server.control;

import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXML;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.ObservableStringValue;
import java.sql.SQLException;
import util.stuff;

public class ServerController {

	@FXML
	Button onBtn;
	@FXML
	Button offBtn;
	@FXML
	Button closeBtn;
	@FXML
	TextArea log;
	@FXML
	ListView users;
	@FXML
	ListView groups;
	@FXML
	AnchorPane ap;

	@FXML
	public void on(ActionEvent event){
		offBtn.setDisable(false);
		onBtn.setDisable(true);
		closeBtn.setDisable(true);

		sc = new ServerControl(this);
	}

	@FXML
	public void off(ActionEvent event){
		offBtn.setDisable(true);
		onBtn.setDisable(false);
		closeBtn.setDisable(false);

		sc.close();
		sc = null;
	}

	@FXML
	public void close(ActionEvent event){
		javafx.application.Platform.exit();
	}

	ServerControl sc;
	String ls;
	SQLException sqle = null;
	
	public ServerController(){
		log = new TextArea("...");
		ls = "";
	}

	public void log(Object o){
		String s = stuff.now()+": ";
		if(o instanceof String){
			s += (String) o;
		}else if(o instanceof Exception){
			Exception e = (Exception) o;
			s += e.getMessage();
			e.printStackTrace();				
		}else{
			System.out.println("wut");
		}
		log.appendText(s+"\n");
		ls += s+"\n";
	}
}